﻿namespace AzureLogProviders.Common
{
    using System;

    using AzureLogProviders.Configuration;

    /// <summary>
    /// Cache Factory
    /// </summary>
    public static class LoggerFactory
    {
        #region Fields

        /// <summary>
        /// Reads and stores log providers from config.
        /// </summary>
        public static readonly ProviderRepository<AzureLogProvider> LogProviders = new ProviderRepository<AzureLogProvider>("logProvider");

        #endregion Fields

        #region Methods

        /// <summary>
        /// Gets the log provider.
        /// </summary>
        /// <param name="logProviderName">Name of the cache provider.</param>
        /// <returns>the log provider</returns>
        public static AzureLogProvider GetLogProvider(string logProviderName)
        {
            if (string.IsNullOrWhiteSpace(logProviderName))
            {
                logProviderName = LogProviders.DefaultProviderName;
            }

            AzureLogProvider provider = LogProviders.Providers[logProviderName];
            return provider;
        }

        /// <summary>
        /// Gets the log provider.
        /// </summary>
        /// <typeparam name="T">The type of log provider</typeparam>
        /// <param name="logProviderName">Name of the log provider.</param>
        /// <returns>the log provider</returns>
        public static T GetLogProvider<T>(string logProviderName)
        {
            if (string.IsNullOrWhiteSpace(logProviderName))
            {
                logProviderName = LogProviders.DefaultProviderName;
            }

            AzureLogProvider provider = LogProviders.Providers[logProviderName];
            return (T)Convert.ChangeType(provider, typeof(T));
        }

        /// <summary>
        /// Gets the log provider.
        /// </summary>
        /// <returns>The log provider</returns>
        public static AzureLogProvider GetLogProvider()
        {
            return LogProviders.Provider;
        }

        /// <summary>
        /// Gets the log provider.
        /// </summary>
        /// <typeparam name="T">The type of log provider</typeparam>
        /// <returns>The log provider</returns>
        public static T GetLogProvider<T>()
        {
            AzureLogProvider provider = LogProviders.Provider;
            return (T)Convert.ChangeType(provider, typeof(T));
        }

        #endregion Methods
    }
}
